<?php

/**
 * uniBoard API BBcode
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_bbcode extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * BBCode ID
	 *
	 * @var integer
	 */
	
	public $bbcode_id 				= 0;
	
	/**
	 * System BBCode?
	 *
	 * @var bool
	 */
	
	public $bbcode_system			= false;
	
	/**
	 * BBCode in Editor??
	 *
	 * @var bool
	 */
	
	public $bbcode_in_editor		= false;
	
	/**
	 * BBCode name
	 *
	 * @var string
	 */
	
	public $bbcode_name				= '';
	
	/**
	 * BBCode tag
	 *
	 * @var string
	 */
	
	public $bbcode_tag				= '';
	
	/**
	 * BBCode type
	 * 0 - [TAG][/TAG]
	 * 1 - [TAG=""][/TAG]
	 * 2 - [TAG]
	 * 
	 * @var integer
	 */
	
	public $bbcode_type				= 0;
	
	/**
	 * BBCode Replace
	 *
	 * @var string
	 */
	
	public $bbcode_replace			= '';
	
	/**
	 * BBCode Template
	 *
	 * @var string
	 */
	
	public $bbcode_template			= '';
	
	/**
	 * Parse BBCode contents?
	 *
	 * @var bool
	 */
	
	public $bbcode_noparse		= false;
	
	/**
	 * Trim BBCode contents?
	 *
	 * @var bool
	 */
	
	public $bbcode_trim			= true;
	
	/**
	 * Break Lines in BBCode?
	 *
	 * @var bool
	 */
	
	public $bbcode_nl2br		= true;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store BBCode ID
		$this -> bbcode_id		= (integer) $target_id;
		
		// Autoload If BBCode ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'bbcodes
			WHERE bbcode_id = \'' . $this -> bbcode_id . '\'
			LIMIT 1');
			
			// BBCode found
			if ( $bbcode_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded				= true;
				
				$this -> bbcode_system	 			= $bbcode_result['bbcode_system'];
				$this -> bbcode_in_editor	 		= $bbcode_result['bbcode_in_editor'];
				$this -> bbcode_name	 			= $bbcode_result['bbcode_name'];
				$this -> bbcode_tag	 				= $bbcode_result['bbcode_tag'];
				$this -> bbcode_type	 			= $bbcode_result['bbcode_type'];
				$this -> bbcode_replace	 			= $bbcode_result['bbcode_replace'];
				$this -> bbcode_template	 		= $bbcode_result['bbcode_template'];
				$this -> bbcode_noparse	 			= $bbcode_result['bbcode_noparse'];
				$this -> bbcode_trim	 			= $bbcode_result['bbcode_trim'];
				$this -> bbcode_nl2br	 			= $bbcode_result['bbcode_nl2br'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_bbcode_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> bbcode_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'bbcodes');
			
				// Store new BBCode id
				$this -> bbcode_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'bbcodes', 'bbcode_id = \'' . $this -> bbcode_id . '\'');
			}
			
			// Flush Cache
			class_cache::flushCache( 'editor_data');
			class_cache::flushCache( 'parsing_data');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'bbcodes', 'bbcode_id = \'' . $this -> bbcode_id . '\'');
		
		// Flush Cache
		class_cache::flushCache( 'editor_data');
		class_cache::flushCache( 'parsing_data');
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_system
	 *
	 * @param bool $new_value
	 */
	
	function setBBCodeSystem( $new_value){
		
		// Store in Api
		$this -> bbcode_system = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['bbcode_system'] = $this -> bbcode_system;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_in_editor
	 *
	 * @param bool $new_value
	 */
	
	function setBBCodeInEditor( $new_value){
		
		// Store in Api
		$this -> bbcode_in_editor = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['bbcode_in_editor'] = $this -> bbcode_in_editor;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_name
	 *
	 * @param string $new_value
	 */
	
	function setBBCodeName( $new_value){
		
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> bbcode_name = $new_value;
			
			// Too long?
			if ( mb_strlen( $new_value) > 255)
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_bbcode_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['bbcode_name'] = $this -> appCore -> db -> strEscape( $this -> bbcode_name);
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_bbcode_name_empty']);
		}
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_tag
	 *
	 * @param string $new_value
	 */
	
	function setBBCodeTag( $new_value){
		
		// Not empty?
		if ( isset( $new_value[0]))
		{
			// Store in Api
			$this -> bbcode_tag = $new_value;
			
			// Too long?
			if ( mb_strlen( $new_value) > 255)
			{
				// Set Error
				$this -> setError( $this -> appCore -> lang['acp_bbcode_tag_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['bbcode_tag'] = $this -> appCore -> db -> strEscape( $this -> bbcode_tag);
			}
		}
		else
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_bbcode_tag_empty']);
		}
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_type
	 *
	 * @param integer $new_value
	 */
	
	function setBBCodeType( $new_value){
		
		// Store in Api
		$this -> bbcode_type = (integer) $new_value;
		
		// Fix incorrect type?
		if ( $this -> bbcode_type < 0 || $this -> bbcode_type > 2)
		{
			$this -> bbcode_type = 0;
		}
		
		// Add to made changes
		$this -> changes['bbcode_type'] = $this -> bbcode_type;

	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_replace
	 *
	 * @param string $new_value
	 */
	
	function setBBCodeReplace( $new_value){
		
		// Store in Api
		$this -> bbcode_replace = $new_value;

		// Add to made changes
		$this -> changes['bbcode_replace'] = $this -> appCore -> db -> strEscape( $this -> bbcode_replace);
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_template
	 *
	 * @param string $new_value
	 */
	
	function setBBCodeTemplate( $new_value){
		
		// Store in Api
		$this -> bbcode_template = $new_value;
		
		// Too long?
		if ( mb_strlen( $new_value) > 255)
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_bbcode_template_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['bbcode_template'] = $this -> appCore -> db -> strEscape( $this -> bbcode_template);
		}
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_noparse
	 *
	 * @param bool $new_value
	 */
	
	function setBBCodeNoParse( $new_value){
		
		// Store in Api
		$this -> bbcode_noparse = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['bbcode_noparse'] = $this -> bbcode_noparse;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_trim
	 *
	 * @param bool $new_value
	 */
	
	function setBBCodeTrim( $new_value){
		
		// Store in Api
		$this -> bbcode_trim = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['bbcode_trim'] = $this -> bbcode_trim;
	
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * Set bbcode_nl2br
	 *
	 * @param bool $new_value
	 */
	
	function setBBCodeNl2Br( $new_value){
		
		// Store in Api
		$this -> bbcode_nl2br = (bool) $new_value;	
	
		// Add to made changes
		$this -> changes['bbcode_nl2br'] = $this -> bbcode_nl2br;
	
	}
	
}